package com.cn.zsc.service;

import com.cn.zsc.db108.dao.AccountAMapper;
import com.cn.zsc.db108.dao.PayMsgMapper;
import com.cn.zsc.db108.model.AccountA;
import com.cn.zsc.db108.model.PayMsg;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.SendStatus;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class PaymentService {

    @Autowired
    AccountAMapper accountAMapper;

    @Autowired
    PayMsgMapper payMsgMapper;

    @Autowired
    DefaultMQProducer producer;

    /**
     * 支付接口
     * @param userId 用户id
     * @param orderId 订单id
     * @param amount 金额
     * @return 1用户不存在;2余额不足 0代表成功
     */
    @Transactional(transactionManager = "tm108", rollbackFor = Exception.class)
    public int payment(int userId, int orderId, BigDecimal amount){
        // 支付操作
        AccountA accountA = accountAMapper.selectByPrimaryKey(userId);
        if (accountA == null)
            return 1;
        if(accountA.getBalance().compareTo(amount) <0) return 2;
        accountA.setBalance(accountA.getBalance().subtract(amount));
        accountAMapper.updateByPrimaryKey(accountA);

        PayMsg payMsg = new PayMsg();
        payMsg.setOrderId(orderId);
        payMsg.setStatus(0);
        payMsg.setFailCnt(0);
        Date date = new Date();
        payMsg.setCreateTime(date);
        payMsg.setCreateUser(userId);
        payMsg.setCreateUser(userId);
        payMsg.setUpdateTime(date);
        payMsgMapper.insertSelective(payMsg);
        return 0;
    }


    /**
     * 支付接口 消息队列
     * @param userId 用户id
     * @param orderId 订单id
     * @param amount 金额
     * @return 1用户不存在;2余额不足 0代表成功
     */
    @Transactional(transactionManager = "tm108", rollbackFor = Exception.class)
    public int paymentMQ(int userId, int orderId, BigDecimal amount) throws Exception {
        // 支付操作
        AccountA accountA = accountAMapper.selectByPrimaryKey(userId);
        if (accountA == null)
            return 1;
        if(accountA.getBalance().compareTo(amount) <0) return 2;
        accountA.setBalance(accountA.getBalance().subtract(amount));
        accountAMapper.updateByPrimaryKey(accountA);

        Message message = new Message();
        message.setTopic("payment");
        message.setKeys(userId + "");
        message.setBody("订单已支付".getBytes());
        try {
            SendResult result = producer.send(message);
            if(result.getSendStatus() == SendStatus.SEND_OK){
                return 0;
            }else {
                throw new Exception("消息发送失败");
            }
        } catch (Exception e) {
           e.printStackTrace();
           throw e;
        }
    }
}
